// 登录
import request from '@/utils/request'
import api from './index'
import qs from 'qs'
import { encryptAes, decryptAes, encryptBase64, decryptBase64 } from '@/utils/encryptUtil'
import { desensitizedData } from '@/utils/desensitized'

export const loadUserInfo = (data ) => {
  //aes加密后自动转为base64
  let encryptStr = encryptAes(JSON.stringify(data))
  return new Promise((resolve, reject) => {
    request({
      url: api.getDoctorUsers,
      method: 'post',
      data: qs.stringify({ 'paramText': encryptStr })
    }).then((res) => {
      if (res.code == '200') {
        res.data = desensitizedData(res.data)
      }
      resolve(res)
    }).catch((e) => {
      reject(e)
    })
  })
}



export const saveUserInfo = (data) => {
  let encryptStr = encryptAes(JSON.stringify(data))

  return request({
    url: api.saveDoctorUser,
    method: 'post',
    data: qs.stringify({ 'paramText': encryptStr })
  })
}

// export const uploadPic = (file, userId, uploadType) => {
//   // const formData = new FormData();
//   let json = getPicJson(userId, uploadType)
//   json.base64 = file.content
//   let encryptStr = encryptAes(JSON.stringify(json))
//   return request({
//     url: api.uploadPic,
//     method: 'post',
//     timeout: 30 * 1000,
//     data: qs.stringify({ 'paramText': encryptStr })
//   })
// }

export const uploadPic = (file, zjhm, uploadType) => {
  const formData = new FormData()
  formData.append('multipartFile', file)
  formData.append('zjhm', zjhm)
  formData.append('type', uploadType)
  return request({
    url: api.uploadPic,
    method: 'post',
    timeout: 30 * 1000,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    urlType: 'pic',
    data: formData
  })
}

export const getPictureList = (userId, picType) => {
  let json = getPicJson(userId, picType)
  let encryptStr = encryptAes(JSON.stringify(json))
  return request({
    url: api.getPictureList,
    method: 'post',
    data: qs.stringify({ 'paramText': encryptStr })
  })
}

export const deletePic = (userId, picType) => {
  let json = getPicJson(userId, picType)
  let encryptStr = encryptAes(JSON.stringify(json))
  return request({
    url: api.deletePicture,
    method: 'post',
    data: qs.stringify({ 'paramText': encryptStr })
  })
}

export const saveDoctorUserPictureUrl = (data) => {
  let encryptStr = encryptAes(JSON.stringify(data))
  return request({
    url: api.saveDoctorUserPictureUrl,
    method: 'post',
    data: qs.stringify({ 'paramText': encryptStr })
  })
}

function getPicJson(userId, picType) {
  let json = {}
  json.permissiontype = '2'
  json.fktable = 'DOCTOR_USER'
  json.fkid = userId
  json.fktablefield = 'id'
  json.fktabledesc = '手机端博士后图片上传'
  json.resourcetype = '9'
  json.picturetype = picType
  json.needFolder = true
  json.resourcestate = '1'
  json.resourcename = '证件照'
  return json
}

export const beforeSaveUserInfo = (data) => {
  delete data.fakexm
  delete data.faketel
  delete data.fakeidcard
  delete data.uploadpicture
  delete data.zj
  delete data.jkm
  delete data.xck
  // data.uploadpicture = data.uploadpicture[0].url //获取图片url
  return data
}

// 撤回审核
export const revokeDoctorUser = (data) => {
  let encryptStr = encryptAes(JSON.stringify(data))

  return request({
    url: api.revokeDoctorUser,
    method: 'post',
    data: qs.stringify({ 'paramText': encryptStr })
  })
}

// 显示按钮
export const getDoctorCutOffTime = (data) => {
  return request({
    url: api.getDoctorCutOffTime,
    method: 'post',
    data: qs.stringify({})
  })
}

// 获取省市区
export const getDoctorAa12List = (data) => {
  let encryptStr = encryptAes(JSON.stringify(data))

  return request({
    url: api.getDoctorAa12List,
    method: 'post',
    data: qs.stringify({ 'paramText': encryptStr })
  })
}

// 查询代办业务
export const checkDoctorZg01 = (data) => {
  let encryptStr = encryptAes(JSON.stringify(data))

  return request({
    url: api.checkDoctorZg01,
    method: 'post',
    data: qs.stringify({ 'paramText': encryptStr })
  })
}

// 查看用户赛务通权限
export const checkDoctorPermission = (data) => {
  let encryptStr = encryptAes(JSON.stringify(data))
  return request({
    url: api.checkDoctorPermission,
    method: 'post',
    data: qs.stringify({ 'paramText': encryptStr })
  })
}
